package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.request.easyeuro.NotifyPayResultRequest;
import com.cjack.frenchexcel.service.EasyEuroService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 充值
 * 对接EasyEuro平台的接口
 */
@RestController
@RequestMapping("/easyEuroClient")
public class EasyEuroEndpoint {
    private final static Logger LOG = LogManager.getLogger( EasyEuroEndpoint.class);
    @Autowired
    private EasyEuroService service;

    @RequestMapping(value = "/notifyPayRes", method = RequestMethod.POST)
    @ResponseBody
    public String notifyPayRes( @RequestBody String requestXml) {
        try {
            LOG.info( "notifyPayRes-----------requestXml------------" + requestXml);
            service.notifyPayRes( requestXml);
            return "success";
        }catch (Exception e){
            LOG.error( e.getMessage());
            return "ERROR";
        }
    }

    /**
     * 后台调用此接口，获取本次支付的二维码
     * @return
     */
    @RequestMapping(value = "/getRechargeQrCode", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getRechargeQrCode( HttpSession session, @RequestParam("totalFee") Double totalFee) {
        Object adminUserId = session.getAttribute( CommonConfig.SESSION_NAME);
        if( adminUserId == null){
            return AjaxResult.ERROR( ErrorCodeDefine.USER_NOT_LOGIN, ErrorCodeDefine.USER_NOT_LOGIN_MSG);
        }
        try {
            return service.getRechargeQrCode( (Long) adminUserId, totalFee);
        }catch (Exception e){
            LOG.error( e.getMessage());
            return AjaxResult.ERROR();
        }
    }

    /**
     * 后台调用此接口轮训 本次支付的结果
     * @return
     */
    @RequestMapping(value = "/queryPayResult", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult queryPayResult( @RequestParam("orderNo") String orderNo) {

        try {
            return AjaxResult.SUCCESS( service.queryOrderStatus( orderNo));
        } catch (Exception e) {
            LOG.error( e.getMessage());
            return AjaxResult.ERROR();
        }
    }


}
